package ui;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import model.AttributeDefinition;
import constant.AttributeDataType;
import service.AttributeService;
import utils.CommonUtil;
import utils.ExcelExportUtil;
import utils.ExcelLoadUtil;

import java.util.List;

public class TemplateManagePage extends Application {
    private TableView<AttributeDefinition> attributeTable;
    private AttributeService attributeService = new AttributeService();

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox(10);
        root.setPadding(new Insets(10));

        // 创建标题
        Label titleLabel = new Label("模板管理");
        titleLabel.setStyle("-fx-font-size: 20px; -fx-font-weight: bold;");

        // 创建按钮区域
        HBox buttonBox = new HBox(10);
        buttonBox.setAlignment(Pos.CENTER_LEFT);
        
        Button addButton = new Button("添加属性");
        Button downloadCustomerButton = new Button("下载客户物流信息模板");
        Button downloadRequirementButton = new Button("下载运输要求模板");
        Button backButton = new Button("返回");
        
        // 设置按钮样式
        String buttonStyle = "-fx-background-color: #5a67d8; -fx-text-fill: white; -fx-arc-width: 200px; " +
                "-fx-arc-height: 100px; -fx-font-size: 16px; -fx-min-width: 180px; -fx-min-height: 40px;";
        addButton.setStyle(buttonStyle);
        downloadCustomerButton.setStyle(buttonStyle);
        downloadRequirementButton.setStyle(buttonStyle);
        backButton.setStyle(buttonStyle);

        // 设置按钮间距
        buttonBox.setSpacing(20);
        
        buttonBox.getChildren().addAll(addButton, downloadCustomerButton, downloadRequirementButton, backButton);

        // 创建属性表格
        attributeTable = new TableView<>();
        setupAttributeTable();

        // 添加所有组件到根容器
        root.getChildren().addAll(titleLabel, buttonBox, attributeTable);

        // 设置按钮事件
        addButton.setOnAction(e -> showAddAttributeDialog());
        downloadCustomerButton.setOnAction(e -> ExcelLoadUtil.loadLogisticsExcel());
        downloadRequirementButton.setOnAction(e -> ExcelLoadUtil.loadRequirementExcel());
        backButton.setOnAction(e -> {
            MenuPage menuPage = new MenuPage();
            menuPage.start(new Stage());
            primaryStage.close();
        });

        // 创建场景
        Scene scene = new Scene(root, 1300, 1000);
        primaryStage.setTitle("模板管理");
        primaryStage.setScene(scene);
        primaryStage.show();

        // 加载数据
        loadAttributes();
    }

    private void setupAttributeTable() {
        // 创建列
        TableColumn<AttributeDefinition, String> displayNameCol = new TableColumn<>("显示名称");
        displayNameCol.setCellValueFactory(cellData -> cellData.getValue().displayNameProperty());

        TableColumn<AttributeDefinition, String> attributeKeyCol = new TableColumn<>("属性键");
        attributeKeyCol.setCellValueFactory(cellData -> cellData.getValue().attributeKeyProperty());

        TableColumn<AttributeDefinition, String> dataTypeCol = new TableColumn<>("数据类型");
        dataTypeCol.setCellValueFactory(cellData -> cellData.getValue().dataTypeProperty());

        TableColumn<AttributeDefinition, String> defaultValueCol = new TableColumn<>("默认值");
        defaultValueCol.setCellValueFactory(cellData -> cellData.getValue().defaultValueProperty());

        // 设置列宽
        displayNameCol.setPrefWidth(200);
        attributeKeyCol.setPrefWidth(200);
        dataTypeCol.setPrefWidth(150);
        defaultValueCol.setPrefWidth(200);

        // 添加列到表格
        attributeTable.getColumns().addAll(displayNameCol, attributeKeyCol, dataTypeCol, defaultValueCol);
    }

    private void showAddAttributeDialog() {
        Dialog<AttributeDefinition> dialog = new Dialog<>();
        dialog.setTitle("添加属性");
        dialog.setHeaderText("请输入新属性的信息");

        // 创建表单
        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 150, 10, 10));

        TextField displayNameField = new TextField();
        TextField attributeKeyField = new TextField();
        ComboBox<AttributeDataType> dataTypeCombo = new ComboBox<>();
        dataTypeCombo.getItems().addAll(AttributeDataType.values());
        TextField defaultValueField = new TextField();

        grid.add(new Label("显示名称:"), 0, 0);
        grid.add(displayNameField, 1, 0);
        grid.add(new Label("属性键:"), 0, 1);
        grid.add(attributeKeyField, 1, 1);
        grid.add(new Label("默认值:"), 0, 2);
        grid.add(defaultValueField, 1, 2);

        dialog.getDialogPane().setContent(grid);

        // 添加按钮
        ButtonType addButtonType = new ButtonType("添加", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(addButtonType, ButtonType.CANCEL);

        // 设置结果转换器
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == addButtonType) {
                if (displayNameField.getText().isEmpty() || attributeKeyField.getText().isEmpty()) {
                    CommonUtil.showAlert("错误", "显示名称、属性键和数据类型不能为空！", Alert.AlertType.ERROR);
                    return null;
                }
                
                AttributeDefinition attr = new AttributeDefinition();
                attr.setDisplayName(displayNameField.getText());
                attr.setAttributeKey(attributeKeyField.getText());
                attr.setDataType(dataTypeCombo.getValue());
                attr.setDefaultValue(defaultValueField.getText());
                return attr;
            }
            return null;
        });

        // 显示对话框并处理结果
        dialog.showAndWait().ifPresent(attr -> {
            if (attributeService.addAttribute(attr) > 0) {
                CommonUtil.showAlert("成功", "属性添加成功！", Alert.AlertType.INFORMATION);
                loadAttributes();
            } else {
                CommonUtil.showAlert("错误", "属性添加失败！", Alert.AlertType.ERROR);
            }
        });
    }

    private void loadAttributes() {
        List<AttributeDefinition> attributes = attributeService.getAllAttributes();
        attributeTable.getItems().clear();
        attributeTable.getItems().addAll(attributes);
    }


} 